home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / misc / amigem.lha / amigem / expansion / startup.c < prev   
Encoding:
C/C++ Source or Header  |  1995-01-29  |  4.2 KB  |  147 lines

  1. #include <exec/resident.h>
  2. #include <exec/execbase.h>
  3. #include <exec/memory.h>
  4. #include <amigem/utils.h>
  5. #include "expansion.h"
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8.  
  9. #include <amigem/fd_lib.h>
  10. #define LIBBASE struct ExpansionBase *ExpansionBase
  11.  
  12. #define malloc                (*(char *(*)(size_t))ft[30])
  13. #define write           (*(ssize_t (*)(int,void *,size_t))ft[63])
  14. #define exit                (*(void (*)(int))ft[35])
  15. #define CacheClearU               (*(void (*)(void))ft[10])
  16.  
  17. long Expansion_Start(void) /* Error return if started as a program */
  18. { return -1; }
  19.  
  20. /* Some globals */
  21. FC3(0,LONG,Expansion_Init,A1,APTR dummy1,D0,BPTR dummy2,A0,void (**ft)(),A6)
  22. ;
  23. extern const long Expansion_End;
  24. extern void (*const expansion_funcTable[])(); 
  25.  
  26. const char Expansion_Name[]=LIB_NAME;
  27. const char Expansion_VStr[]=LIB_VERSTRING(LIB_NAME,LIB_VERSION,LIB_REVISION,LIB_DATE);
  28.  
  29. const struct Resident Expansion_RomTag=
  30. {
  31.   RTC_MATCHWORD,
  32.   (struct Resident *)&Expansion_RomTag,
  33.   (APTR)&Expansion_End,
  34.   0,
  35.   LIB_VERSION,
  36.   NT_LIBRARY,
  37.   110,
  38.   (char *)Expansion_Name,
  39.   LIB_ID(Expansion_VStr),
  40.   (APTR)&__Expansion_Init
  41. };
  42.  
  43. FC3(0,LONG,Expansion_Init,A1,APTR dummy1,D0,BPTR dummy2,A0,void (**ft)(),A6)
  44. {
  45.   struct ExpansionBase *ExpansionBase;
  46.   UBYTE *mem,*fmem;
  47.   mem=(UBYTE *)malloc(LOCAL_MEM_SIZE+sizeof(struct MemChunk)-1);
  48.   if(mem==NULL)
  49.   {
  50.     write(STDERR_FILENO,"Memory exhausted\n",17);
  51.     exit(20);
  52.   }
  53.   fmem=mem=(UBYTE *)ALIGN(mem,sizeof(struct MemChunk));
  54.  
  55.   { /* Build library */
  56.     APTR *f;
  57.     UBYTE *t;
  58.     ULONG s=0;
  59.  
  60.     f=(APTR)expansion_funcTable;
  61.     while((long)*f++!=-1)
  62.       s+=LIB_VECTSIZE;
  63.  
  64.     s=(ULONG)ALIGN(s,sizeof(ULONG));
  65.     ExpansionBase=(struct ExpansionBase *)(fmem+=s);
  66.     fmem+=sizeof(struct ExpansionBase);
  67.     fmem=(UBYTE *)ALIGN(fmem,sizeof(struct MemChunk));
  68.  
  69.     f=(APTR)expansion_funcTable;
  70.     t=(char *)ExpansionBase;
  71.     while((long)*f!=-1)
  72.     {
  73.       t-=LIB_VECTSIZE;
  74.       MINSETFUNCTION(t,*f);
  75.       f++;
  76.     }
  77.     CacheClearU();
  78.  
  79.     ExpansionBase->LibNode.lib_Node.ln_Type    =NT_LIBRARY;
  80.     ExpansionBase->LibNode.lib_Node.ln_Pri    =0;
  81.     ExpansionBase->LibNode.lib_Node.ln_Name    =(char *)Expansion_Name;
  82.     ExpansionBase->LibNode.lib_Flags        =LIBF_CHANGED|LIBF_SUMUSED;
  83.     ExpansionBase->LibNode.lib_NegSize        =s;
  84.     ExpansionBase->LibNode.lib_PosSize        =sizeof(struct ExpansionBase);
  85.     ExpansionBase->LibNode.lib_Version        =LIB_VERSION;
  86.     ExpansionBase->LibNode.lib_Revision        =LIB_REVISION;
  87.     ExpansionBase->LibNode.lib_IdString        =LIB_ID(Expansion_VStr);
  88.     ExpansionBase->LibNode.lib_OpenCnt        =1; /* Count exec as one opener */
  89.     ExpansionBase->LocalMemStart        =(ULONG)mem;
  90.     ExpansionBase->LocalMemSize            =LOCAL_MEM_SIZE;
  91.     NEWLIST(&ExpansionBase->ConfigDevList);
  92.     NEWLIST(&ExpansionBase->MountList);
  93.     /*
  94.      * exec.library puts it's own address into ExpansionBase->SysBase and initializes
  95.      * the ExpansionBase->ConfigBinding signal semaphore. The first field cannot be
  96.      * initialized by expansion.library itself (because exec does not yet exist)
  97.      * the second prevents doubling the InitSemaphore function (and thus possible bugs).
  98.      */
  99.   }
  100.  
  101.   { /* Add the jumptable for the unix kernel functions as some hardware */
  102.     struct ConfigDev *c;
  103.     c=(struct ConfigDev *)fmem;
  104.     c->cd_Flags            =0;
  105.     c->cd_Rom.er_Type        =1;
  106.     c->cd_Rom.er_Product    =88;
  107.     c->cd_Rom.er_Manufacturer    =2011;
  108.     c->cd_Rom.er_SerialNumber    =42424242;
  109.     c->cd_BoardAddr        =ft;
  110.     c->cd_BoardSize        =1;
  111.     c->cd_Driver        =NULL;
  112.     c->cd_NextCD        =NULL;
  113.     ADDTAIL(&ExpansionBase->ConfigDevList,&c->cd_Node);
  114.   }
  115.   {/* Looking for exec */
  116.     UWORD *w=(UWORD *)Expansion_RomTag.rt_EndSkip; /* Top of scanning area */
  117.  
  118.     while(((struct Resident *)w)->rt_MatchWord!=RTC_MATCHWORD||
  119.           ((struct Resident *)w)!=((struct Resident *)w)->rt_MatchTag)
  120.       w++;
  121.     /* Even if it doesn't look like: This initializes exec!!! */
  122.     Expansion_Init(((struct Resident *)w)->rt_Init,NULL,0,(void (**)())ExpansionBase);
  123.   }
  124.  
  125.   return 1; /* Never reached */
  126. }
  127.  
  128. FD1(1,struct Library *,Expansion_Open,ULONG version,D0)
  129. {
  130.   ExpansionBase->LibNode.lib_OpenCnt++;
  131.   return &ExpansionBase->LibNode;
  132. }
  133.  
  134. FD0(2,BPTR,Expansion_Close)
  135. {
  136.   ExpansionBase->LibNode.lib_OpenCnt--;
  137.   return 0;
  138. }
  139.  
  140. FD0(3,BPTR,Expansion_Expunge)
  141. {
  142.   return 0; /* Do not expunge expansion.library */
  143. }
  144.  
  145. FD0(4,ULONG,Expansion_Null)
  146. { return 0; }
  147.